package org.lex.notepad.ui;

import java.awt.event.KeyEvent;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import org.lex.notepad.Notepad;
import org.lex.notepad.ui.action.AboutAction;
import org.lex.notepad.ui.action.CharsetAction;
import org.lex.notepad.ui.action.PreferencesAction;
import org.lex.notepad.ui.action.edit.FindAction;
import org.lex.notepad.ui.action.edit.GotoLineAction;
import org.lex.notepad.ui.action.edit.InsertDateTimeAction;
import org.lex.notepad.ui.action.edit.ReplaceAction;
import org.lex.notepad.ui.action.file.CloseAction;
import org.lex.notepad.ui.action.file.ExitAction;
import org.lex.notepad.ui.action.file.NewFileAction;
import org.lex.notepad.ui.action.file.OpenAction;
import org.lex.notepad.ui.action.file.SaveAction;
import org.lex.notepad.ui.action.file.SaveAsAction;

/**
 * ���˵���
 */
public class MainMenuBar extends JMenuBar {
	private Notepad notepad = null;
	// =============================
	private JMenu fileMenu = null;
	// =====================================
	private JMenu editMenu = null;
	private JMenuItem findNextMenuItem = null;
	// =====================================
	private JMenu formatMenu = null;
	// =======================================
	private JMenu helpMenu = null;

	public MainMenuBar() {
		super();
		this.notepad = Notepad.getApplication();
		this.initialize();
	}

	private void initialize() {
		this.add(this.getFileMenu());
		this.add(this.getEditMenu());
		this.add(this.getFormatMenu());
		this.add(this.getHelpMenu());
	}

	// =========================
	// File Menu
	// =========================
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setMnemonic(KeyEvent.VK_F);
			fileMenu.setText("File");
			fileMenu.add(new NewFileAction());
			fileMenu.add(new OpenAction());
			fileMenu.addSeparator();
			fileMenu.add(new SaveAction());
			fileMenu.add(new SaveAsAction());
			fileMenu.add(new CloseAction());
			fileMenu.addSeparator();
			fileMenu.add(new ExitAction());
		}
		return fileMenu;
	}

	// ==========================
	// Edit Menu
	// ==========================
	public JMenu getEditMenu() {
		if (null == editMenu) {
			editMenu = new JMenu("Edit");
			editMenu.setMnemonic('e');
			editMenu.add(new FindAction());
			editMenu.add(this.getFindNextMenuItem());
			editMenu.add(new ReplaceAction());
			editMenu.add(new GotoLineAction());
			editMenu.add(new InsertDateTimeAction());
			editMenu.addSeparator();
			editMenu.add(new PreferencesAction());
		}
		return this.editMenu;
	}

	public JMenuItem getFindNextMenuItem() {
		if (null == findNextMenuItem) {
			findNextMenuItem = new JMenuItem("Find next");
		}
		return this.findNextMenuItem;
	}

	// ==========================
	// Format Menu
	// ==========================
	private JMenu getFormatMenu() {
		if (formatMenu == null) {
			formatMenu = new JMenu();
			formatMenu.setText("Format");
			formatMenu.setMnemonic(KeyEvent.VK_O);
			formatMenu.add(new JCheckBoxMenuItem(notepad.getEditor().getLineWrapAction()));
			formatMenu.add(new JCheckBoxMenuItem(notepad.getEditor().getWordWrapAction()));
			formatMenu.add(notepad.getEditor().getFontAction());
			formatMenu.add(notepad.getEditor().getTabSizeAction());
		}
		return formatMenu;
	}

	// ========================
	// Help Menu
	// ========================
	public JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setMnemonic(KeyEvent.VK_H);
			helpMenu.setText("Help");
			helpMenu.add(new CharsetAction());
			helpMenu.addSeparator();
			helpMenu.add(new AboutAction());
		}
		return helpMenu;
	}
}
